<?php

namespace app\admin\model;

use app\common\model\Equipment as EquipmentModel;
use think\Db;

/**
 * 打印设备模型
 */
class Equipment extends EquipmentModel
{
    /**
     * 添加
     */
    public function add(array $data)
    {
		if($this->detail([
			'uuid' => $data['uuid'],
			'brand' => $data['brand']
		])){
			$this->error = '该设备已存在';
            return false;
		}
		//对对机
		if($data['brand']==10){
			if(strlen($data['uuid'])!= 16){
				$this->error = '设备编号因该是16位';
				return false;
			}
		}
		//飞鹅
		if($data['brand']==20){
			if(strlen($data['uuid'])!= 9){
				$this->error = '设备编号因该是9位';
				return false;
			}
		}
		//易联云
		if($data['brand']==30){
			if(strlen($data['uuid'])!= 10){
				$this->error = '设备编号因该是10位';
				return false;
			}
		}
        // 开启事务
        Db::startTrans();
        try {
            // 添加
            $this->allowField(true)->save($data);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
        }
        return false;
    }
	
    /**
     * 编辑
     */
    public function edit($data)
    {
		if($this->detail([
			'uuid' => $data['uuid'],
			'brand' => $data['brand']
		])){
			$this->error = '该设备已存在';
            return false;
		}
		//对对机
		if($data['brand']==10){
			if(strlen($data['uuid'])!= 16){
				$this->error = '设备编号因该是16位';
				return false;
			}
		}
		//飞鹅
		if($data['brand']==20){
			if(strlen($data['uuid'])!= 9){
				$this->error = '设备编号因该是9位';
				return false;
			}
		}
		//易联云
		if($data['brand']==30){
			if(strlen($data['uuid'])!= 10){
				$this->error = '设备编号因该是10位';
				return false;
			}
		}
        // 开启事务
        Db::startTrans();
        try {
            // 保存
            $this->allowField(true)->save($data);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }


    /**
     * 删除
     */
    public function remove()
    {
        // 开启事务处理
        Db::startTrans();
        try {
            // 删除
            $this->delete();
            // 事务提交
            Db::commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            Db::rollback();
            return false;
        }
    }

}
